cmake_minimum_required(VERSION 3.31)

project(led_asm)

enable_language(ASM)

if (BUILD_PLATFORM STREQUAL "IMX6ULL")
    set(TOOLPATH arm-none-eabi-)
else()
    MESSAGE(STATUS "Unsupported platform!")
endif()

if (WIN32)
    MESSAGE(STATUS "Compiling in Window System!")
    set(CMAKE_C_COMPILER ${TOOLPATH}gcc.exe)
    set(CMAKE_CXX_COMPILER ${TOOLPATH}g++.exe)
    set(CMAKE_ASM_COMPILER ${TOOLPATH}gcc.exe)
    set(CMAKE_AR ${TOOLPATH}ar.exe)
    set(CMAKE_OBJCOPY ${TOOLPATH}objcopy.exe)
    set(CMAKE_OBJDUMP ${TOOLPATH}objdump.exe)
    set(CMAKE_SIZE ${TOOLPATH}size.exe)
elseif (UNIX)
    MESSAGE(STATUS "Compiling in UNIX-like OS!")
    set(CMAKE_C_COMPILER ${TOOLPATH}gcc)
    set(CMAKE_CXX_COMPILER ${TOOLPATH}g++)
    set(CMAKE_ASM_COMPILER ${TOOLPATH}gcc)
    set(CMAKE_AR ${TOOLPATH}ar)
    set(CMAKE_OBJCOPY ${TOOLPATH}objcopy)
    set(CMAKE_OBJDUMP ${TOOLPATH}objdump)
    set(CMAKE_SIZE ${TOOLPATH}size)
else ()
    MESSAGE(STATUS "Unsupported system!")
endif ()


#*******************************************************
# 生成 compile_commands.json
#*******************************************************
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_compile_options(-marm)                  # 全局强制使用 ARM 指令集
add_compile_options(-fno-stack-protector)   # 禁用栈保护
add_compile_options(
    -mcpu=cortex-a7                         # 指定Cortex-A7核心
    -mfpu=neon-vfpv4                        # 启用VFPv4硬件浮点单元[3](@ref)
    -mfloat-abi=hard                        # 强制硬件浮点调用约定
    -nostdlib
)

# 添加可执行文件
add_executable(${CMAKE_PROJECT_NAME}
    ${CMAKE_CURRENT_SOURCE_DIR}/start.S
)

# 在 add_executable 之后添加以下链接选项
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
    -nostdlib                                       # 禁用标准库
    -nostartfiles                                   # 禁用标准启动文件（如Scrt1.o）
    -Wl,--gc-sections                               # 启用无用代码优化
)

# 输出 elf、hex 和 bin
set(HEX_FILE ${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin)
set(LST_FILE ${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.lst)

set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
    SUFFIX ".elf"  # 强制添加后缀
    OUTPUT_NAME "${CMAKE_PROJECT_NAME}"
)

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${PROJECT_NAME}> ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${PROJECT_NAME}> ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJDUMP} -D $<TARGET_FILE:${PROJECT_NAME}> > ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.asm
)

#*******************************************************
# 生成基于 SD 卡 imx 文件
#*******************************************************
# 步骤1：合并 header 和 xxx.bin生成 xxx.imx
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.imx
    COMMAND ${CMAKE_COMMAND} -E cat 
        ${CMAKE_CURRENT_SOURCE_DIR}/../tools/uuu/header 
        ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin
        > ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.imx
    DEPENDS 
        ${CMAKE_CURRENT_SOURCE_DIR}/../tools/uuu/header
        ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin
    COMMENT "Generating IVT Boot Image ${CMAKE_PROJECT_NAME}.imx"
)

# 创建自定义目标
add_custom_target(build_imx ALL 
    DEPENDS
        ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.imx
)
